home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 3 / BBS in a box - Trilogy III.iso / Files / Bus / T-Z / VCR+(app+src) Folder / Sources / PStringStuff.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-01  |  3.2 KB  |  144 lines  |  [TEXT/KAHL]

  1. #include    "PStringStuff.h"
  2.  
  3. //    various Pascal string manipulation utilities since we're not using C strings.
  4.  
  5. //    CopyPString copies the first string to the second (snarfed from TBUtilities.c
  6. //    from Symantec)
  7.  
  8. //    ConcatPStrings concatenates the second string onto the end of the first
  9. //    (snarfed from TBUtilities.c from Symantec).
  10.  
  11. //    int2Str converts an int into a string, correctly handling negative numbers.
  12.  
  13. //    PsubStr takes a string, a start position, and an end position and returns
  14. //    the string piece. Note that this is destructive; if you need to preserve the
  15. //    original, call the routine on a copy of the string.
  16.  
  17. //    Pstrcmp does NOT work like its C equivalent. It returns TRUE if they
  18. //    are the same, and FALSE if they are not. The C equivalent returns 0 (FALSE)
  19. //    if they are the same, -1 if string 2 < string 1, and 1 if string 2  >  string1.
  20.  
  21. //    stripColons assumes you pass it something like a pathname, and want the part
  22. //    AFTER the last colon. Note that it is destructive of the string you pass it.
  23.  
  24.  
  25. void    CopyPString(ConstStr255Param srcString, Str255 destString)
  26. {
  27.     BlockMove(srcString, destString, srcString[0] + 1L);
  28. }
  29.  
  30.  
  31. void    Pcharcat(Str255 srcString, char addThis)
  32. {
  33.     if (srcString[0] < 255)
  34.     {
  35.         srcString[0] += 1;
  36.         srcString[srcString[0]] = addThis;
  37.     }
  38. }
  39.  
  40.  
  41. void    ConcatPStrings(Str255 first, ConstStr255Param second)
  42. {
  43.     
  44.         short charsToCopy;
  45.     
  46.     //    Truncate if concatenated string would be longer than 255 chars.
  47.     
  48.     charsToCopy = Min(second[0], 255 - first[0]);
  49.     BlockMove(second + 1, first + first[0] + 1, (long) charsToCopy);
  50.     first[0] += charsToCopy;
  51. }
  52.  
  53. void    int2Str(short theNum, Str255 theString)
  54. {
  55.     NumToString((long)Abs(theNum),theString);
  56.     
  57.     if (theNum < 0)
  58.     {
  59.             Str255    swapStr;        //    we need a temporary swap string
  60.         
  61.         CopyPString("\p-",swapStr);
  62.         ConcatPStrings(swapStr,theString);
  63.         CopyPString(swapStr,theString);
  64.     }
  65. }
  66.  
  67. void PsubStr(unsigned char *dest, short startPos, short endPos)
  68.         
  69. {
  70.         short         i;
  71.         Str255        tempStr;
  72.  
  73.     if (startPos < 1)                        //    startPos must be greater than zero
  74.         startPos = 1;
  75.     if (endPos > dest[0])                        //    endPos must be a valid part of the string
  76.         endPos = dest[0];
  77.     else if (endPos < startPos)                //    endPos must be at least startPos
  78.         endPos = startPos;
  79.  
  80.     tempStr[0] = endPos - startPos +1;            //    example…5-3+1 = 3 chars (#s 3,4 & 5)
  81.     for (i = startPos; i <= endPos; i++)
  82.     {
  83.         tempStr[i-startPos+1] = dest[i];
  84.     }
  85.     CopyPString(tempStr,dest);
  86. }
  87.  
  88. Boolean Pstrcmp(unsigned char *string1, unsigned char *string2)
  89. {
  90.         short    i;
  91.     
  92.     if (*string1 != *string2) 
  93.         return FALSE;
  94.     
  95.     for (i = 0; i <= *string1; i++)
  96.     {
  97.         if (string1[i] != string2[i])
  98.             return FALSE;
  99.     }
  100.     return TRUE;
  101. }
  102.  
  103. void    stripColons(Str255 theString)
  104. {
  105.         short    i;
  106.         short    j = theString[0];
  107.         
  108.     if (theString[0] != 0)
  109.     {
  110.     
  111.         for (i=theString[0];i>0;i--)
  112.         {
  113.             if (theString[i] == ':')
  114.                 break;
  115.             if (theString[i] == '\r')
  116.                 j = i;
  117.         }
  118.         
  119.         if (i != 0)
  120.         {
  121.             PsubStr(theString,i+1,j);
  122.         }
  123.     }
  124. }
  125.  
  126. int    findString(Str255 strToFind, Str255 strToSearch)
  127. {
  128.         //    returns the position of strToFind in strToSearch
  129.         
  130.         short    i,j;
  131.         
  132.     for(i=1; i <= strToSearch[0]-strToFind[0]; i++)
  133.     {
  134.         j=0;
  135.         while((strToFind[j+1] == strToSearch[i+j]) && (j < strToFind[0]))
  136.             j++;
  137.         
  138.         if (j == strToFind[0])
  139.             return i;
  140.     }
  141.     return 0;
  142.     
  143. }
  144.